package com.probiz.estore.product.service.impl;

import javax.servlet.http.HttpServletRequest;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.product.ProductReview;
import com.probiz.estore.common.model.product.ProductReviewAndVoteVO;
import com.probiz.estore.common.model.product.ReviewVote;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.ReviewVoteDao;
import com.probiz.estore.product.service.ProductReviewManager;
import com.probiz.estore.product.service.ReviewVoteManager;
import com.probiz.estore.webapp.util.RequestContext;


/**
 * Manager implementation for ReviewVote, responsible for business processing, and communicate between web and persistence layer.
 */
public class ReviewVoteManagerImpl extends GenericManagerImpl<ReviewVote> implements ReviewVoteManager {

	private ReviewVoteDao reviewVoteDao = null;
	
	private ProductReviewManager productReviewManager = null;

	/**
	 * @param reviewVoteDao
	 *            the reviewVoteDao to set
	 */
	public void setReviewVoteDao(ReviewVoteDao reviewVoteDao) {
		this.reviewVoteDao = reviewVoteDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = reviewVoteDao;
	}

	public void setProductReviewManager(ProductReviewManager productReviewManager) {
		this.productReviewManager = productReviewManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(ReviewVote entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(ReviewVote entity) {

	}

	public boolean isVoteAlready(Integer productReviewId, Integer customerId) {
		return reviewVoteDao.isVoteAlready(productReviewId, customerId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ReviewVoteManager#doVote(javax.servlet.http.HttpServletRequest, java.lang.Integer, java.lang.Short)
	 */
	public ProductReviewAndVoteVO doVote(HttpServletRequest request,Integer productReviewId, Short chosen){
		ProductReviewAndVoteVO productReviewAndVoteVO = new ProductReviewAndVoteVO();
		ProductReview productReview=productReviewManager.getById(productReviewId);
		//检查用户是否已经登录了
		if(RequestContext.isAnonymousUser()){
			productReviewAndVoteVO.setResult(ProductReviewAndVoteVO.VOTE_RESULT_NOTLOGIN);
			productReviewAndVoteVO.setProductReview(productReview);
			return productReviewAndVoteVO;
		}
		
		//检查是否对自己发表的评论进行投票
		if(productReview.getCustomer()!=null && productReview.getCustomerId().intValue()==RequestContext.getCurrentUserId().intValue()){
			productReviewAndVoteVO.setResult(ProductReviewAndVoteVO.VOTE_RESULT_NOVOTESELF);
			productReviewAndVoteVO.setProductReview(productReview);
			return productReviewAndVoteVO;
		}
		//检查用户是否对该评论已经进行了投票
		if(this.isVoteAlready(productReviewId, RequestContext.getCurrentUserId())){
			productReviewAndVoteVO.setResult(ProductReviewAndVoteVO.VOTE_RESULT_VOTEALREADY);
			productReviewAndVoteVO.setProductReview(productReview);
			return productReviewAndVoteVO;
		}else{
			productReviewAndVoteVO.setResult(ProductReviewAndVoteVO.VOTE_RESULT_SUCCESS);
		}
		if (chosen.equals(Constants.FLAG_TRUE)) {
			productReview.setUsefulCount(productReview.getUsefulCount()+1);
		}else{
			productReview.setUnusefulCount(productReview.getUnusefulCount()+1);
		}
		ReviewVote reviewVote=new ReviewVote();
		reviewVote.setCustomerId(RequestContext.getCurrentUserId());
		reviewVote.setProductReview(productReview);
		reviewVote.setChosen(chosen);
		this.save(reviewVote);
		productReviewAndVoteVO.setProductReview(productReview);
		productReviewAndVoteVO.setReviewVote(reviewVote);
		return productReviewAndVoteVO;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ReviewVoteManager#getByReviewIdAndCustomerId(java.lang.Integer, java.lang.Integer)
	 */
	public ReviewVote getByReviewIdAndCustomerId(Integer productReviewId, Integer customerId){
		return reviewVoteDao.getByReviewIdAndCustomerId(productReviewId, customerId); 
	}
	

}
